package core

import (
	"fmt"
	"log"

	"server/config"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var DB = InitMySQL()

func InitMySQL() *gorm.DB {
	m := config.Config.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s&parseTime=True&loc=Local", m.Username, m.Password, m.Host, m.Port, m.Database, m.Charset)
	db, err := gorm.Open(mysql.New(mysql.Config{
		DSN:                       dsn,
		DefaultStringSize:         256,
		DisableDatetimePrecision:  false,
		DontSupportRenameIndex:    true,
		DontSupportRenameColumn:   true,
		SkipInitializeWithVersion: false,
	}), &gorm.Config{})
	if err != nil {
		log.Fatalln("连接Mysql数据库失败", err)
	}

	return db
}
